package com.myself;

public class Singleton {
    public static void main(String[] args) {

    }
}


class SingletonInstance{
    private SingletonInstance(){}
    private static SingletonInstance singletonInstance = new SingletonInstance();
    public static SingletonInstance getInstance(){
        return singletonInstance;
    }
}

class SingletonInstance2{
    // 构造函数私有化
    private SingletonInstance2(){}
    // 创建一个实例变量
    private static volatile SingletonInstance2 singletonInstance2;

    public static synchronized SingletonInstance2 getInstance(){
        if(singletonInstance2 == null){
            synchronized (SingletonInstance2.class){
                if(singletonInstance2 == null){
                    singletonInstance2 = new SingletonInstance2();
                }
            }
        }
        return singletonInstance2;
    }

}
